<?php

namespace App\Admin\Controllers;

use App\Models\Grade;
use App\Models\ClassCourse;
use App\Models\Teacher;
use App\Models\Student;
use App\Models\Semester;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use App\Models\Details;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use App\admin\Extensions\InsertTCQE;


class TeacherCQEController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('综合素质评价')
            ->description('教师评价')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('综合素质评价')
            ->description(' ')
            ->body($this->detail($id));

    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('综合素质评价')
            ->description(' ')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('综合素质评价')
            ->description(' ')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Student);

        $user_id = Admin::user()->id;
        $teacher = Teacher::where('user_id',$user_id)->first();
        $teacher_id = $teacher->id;
        $grades = ClassCourse::where('course_teacher',$teacher_id)->get();
        $gradeIds = [];

        foreach ($grades as $grade){
            array_push($gradeIds, $grade->class_id);
        }
//        dd($gradeIds);
        $grid->model()->whereIn('class_id',$gradeIds)->where('id','>',0);

        $grid->id('Id')->sortable();
        $grid->column('当前学期')->display(function () {
           $semester = Semester::where('is_semester',1)->first();
           return $semester->name;
        });
        $grid->column('class.class_name' ,'班级');
        $grid->student_id('学号');
        $grid->seat('座号');
        $grid->name('学生');
//        $grid->created_at('创建日期');
//        $grid->updated_at('修改日期');
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();
            $id = $actions->getKey();
            $appraiser_type = '1';

            $actions->append(new InsertTCQE($id,$appraiser_type));
        });
        $grid->paginate(10);
        $grid->disableCreateButton();
        $grid->disableFilter();
//        $grid->disableActions();
        $grid->disableRowSelector();
        $grid->disableExport();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {

    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Grade);

        return $form;
    }
}
